What does TensorFlow do?

TensorFlow is a way of representing computation without actually performing it until asked. In this sense, it is a form of lazy computing, and it allows for some great improvements to the running of code:

  • Faster computation of complex variables
  • Distributed computation across multiple systems, including GPUs.
  • Reduced redundency in some computations

Add two numbers


In [1]:
import tensorflow as tf

x = tf.constant(35, name='x')
y = tf.Variable(x + 5, name='y')

print(y)


<tensorflow.python.ops.variables.Variable object at 0x7f149009fa58>

In [2]:
x = tf.constant(35, name='x')
y = tf.Variable(x + 5, name='y')

model = tf.initialize_all_variables()

with tf.Session() as session:
    session.run(model)
    print(session.run(y))


40

Exercise

1.


In [3]:
import tensorflow as tf
x = tf.constant([35, 40, 45], name='x')
y = tf.Variable(x + 5, name='y')


model = tf.initialize_all_variables()

with tf.Session() as session:
	session.run(model)
	print(session.run(y))


[40 45 50]

2.


In [16]:
import numpy as np
x=np.random.rand(10)
y=tf.Variable(5*x**2,name='y')
model = tf.initialize_all_variables()

with tf.Session() as session:
    session.run(model)
    print(session.run(y))


[ 3.35509394  1.70215193  1.05872909  1.76074012  4.60089264  2.84141962
  0.40380513  3.7792622   2.61410542  0.08275855]

tensorboard


In [26]:
import tensorflow as tf

x = tf.constant(35, name='x')
print(x)
y = tf.Variable(x + 5, name='y')

with tf.Session() as session:
    merged = tf.merge_all_summaries()
    writer = tf.train.SummaryWriter("", session.graph)
    model = tf.initialize_all_variables()
    session.run(model)
    print(session.run(y))


Tensor("x_5:0", shape=(), dtype=int32)
40

In [20]:
import matplotlib.image as mpimg

# First, load the image
filename = "MarshOrchid.jpg"
image = mpimg.imread(filename)

# Print out its shape
print(image.shape)


(5528, 3685, 3)

In [21]:
import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()



In [25]:
import tensorflow as tf
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

# First, load the image again
filename = "MarshOrchid.jpg"
image = mpimg.imread(filename)

# Create a TensorFlow Variable
x = tf.Variable(image, name='x')

model = tf.initialize_all_variables()

with tf.Session() as session:
    x = tf.transpose(x, perm=[1, 0, 2])
    session.run(model)
    result = session.run(x)


plt.imshow(result)
plt.show()



In [ ]: